package com.bawei.module_live.version2.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_live.R
import com.bawei.module_live.version2.mvi.model.entity.LiveEntity2


class LiveAdapter(
    private val onItemClick: (channelId: String) -> Unit // 新增点击回调
) :  RecyclerView.Adapter<LiveAdapter.LiveViewHolder>() {

    private val data = arrayListOf<LiveEntity2>()


    // 添加点击事件接口
    var onItemClickListener: ((LiveEntity2) -> Unit)? = null


    // 修改1: 使用 DiffUtil 进行高效局部刷新
    fun setData(newData: List<LiveEntity2>) {
        val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
            override fun getOldListSize() = data.size
            override fun getNewListSize() = newData.size
            override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean {
                return data[oldPos].id == newData[newPos].id // 假设 VideoEntity 有唯一ID
            }
            override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean {
                return data[oldPos] == newData[newPos]
            }
        })
        data.clear()
        data.addAll(newData)
        diffResult.dispatchUpdatesTo(this) // 局部刷新
    }

    // 修改2: 添加数据时使用局部刷新
    fun addData(newData: List<LiveEntity2>) {
        val startPosition = data.size
        data.addAll(newData)
        notifyItemRangeInserted(startPosition, newData.size) // 只刷新新增部分
    }

    // 修改3: 单独更新某个item
    fun updateItem(position: Int, newItem: LiveEntity2) {
        if (position in 0 until data.size) {
            data[position] = newItem
            notifyItemChanged(position) // 只刷新指定位置
        }
    }

    // 修改4: 删除item时局部刷新
    fun removeItem(position: Int) {
        if (position in 0 until data.size) {
            data.removeAt(position)
            notifyItemRemoved(position) // 只刷新删除位置
        }
    }


    override fun onBindViewHolder(holder: LiveViewHolder, position: Int) {
        TODO("Not yet implemented")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveViewHolder {
        return LiveViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_live2, parent, false)
        )
    }


    override fun getItemCount() = data.size


    inner class LiveViewHolder(view: View) : RecyclerView.ViewHolder(view) {
//        val progressBar: ProgressBar = view.findViewById(R.id.progressBar)
//        val loadingText: TextView = view.findViewById(R.id.loadingText)
//
//        init {
//            // 设置默认状态
//            loadingText.text = "正在拼命加载中..."
//            progressBar.visibility = View.VISIBLE
//        }
//
//        fun bind(isLoading: Boolean) {
//            // 如果需要动态更新可以调用这个方法
//            progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
//            loadingText.text = if (isLoading) "正在拼命加载中..." else "加载完成"
//        }
    }
}